package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.mapper.PermissionMapper;
import com.itcast.mapper.RoleMapper;
import com.itcast.mapper.UserMapper;
import com.itcast.pojo.Permission;
import com.itcast.pojo.Role;
import com.itcast.pojo.User;
import com.itcast.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * 用户服务
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public User findByUsername(String username) {
        //根据username查询User表的数据
        User user = userMapper.findByUsername(username);
        if (user == null){
            //用户不存在
            return null;
        }
        Integer id = user.getId();
        //根据id查询角色Role表
        Set<Role> roles = roleMapper.findByIdRole(id);
        for (Role role : roles) {
            Integer roleId = role.getId();
            //根据id查询权限表数据
            Set<Permission> permissions = permissionMapper.findByIdUser(id);
            //让角色关联权限
            role.setPermissions(permissions);
        }
        user.setRoles(roles);
        return user;
    }
}
